package cn.hs.lyxf.service.impl;

import cn.hs.lyxf.domain.ShopType;
import cn.hs.lyxf.exception.GlobalErrorCode;
import cn.hs.lyxf.mapper.ShopTypeMapper;
import cn.hs.lyxf.service.IShopTypeService;
import cn.hs.lyxf.utils.AssertUtil;
import cn.hs.lyxf.vo.ShopTypeCrumb;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-12-11
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Override
    public List<ShopTypeCrumb> getCrumbs(Long shopTypeId) {
// 业务校验
ShopType shopType = selectById(shopTypeId);
AssertUtil.isNotNull(shopType, GlobalErrorCode.ERROR);
// 所有父级id,按.切割
String path = shopType.getPath();
AssertUtil.isNotNull(path, GlobalErrorCode.ERROR);
String[] split = path.split("\\.");
List<ShopTypeCrumb> shopTypeCrumbs = new ArrayList<>();
Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList()).forEach(shopTypeIdTmp -> {
    // 根据id查询数据
    ShopType shopTypeById = selectById(shopTypeIdTmp);
    AssertUtil.isNotNull(shopTypeById, GlobalErrorCode.ERROR);
    Wrapper<ShopType> wrapper = new EntityWrapper<>();
    wrapper.eq("parent_id", shopTypeById.getId());
    List<ShopType> list = selectList(wrapper);
    ShopTypeCrumb shopTypeCrumb = new ShopTypeCrumb(shopTypeById, list);
    shopTypeCrumbs.add(shopTypeCrumb);
});
return shopTypeCrumbs;
    }
}
